import { Switch } from "antd";
import { useState } from "react";
import PropTypes from "prop-types";

/**
 * 开关选择器
 * onChange事件加载效果
 *
 *
 * @returns SwitchLoadingComponent
 */
const SwitchLoadingComponent = ({ onChange, ...resProps }) => {
  const [loading, setLoading] = useState(false);

  const handleChange = (value) => {
    setLoading(true);
    onChange && onChange(value).finally(() => setLoading(false));
  };

  return <Switch {...resProps} loading={loading} onChange={handleChange} />;
};
SwitchLoadingComponent.propTypes = {
  onChange: PropTypes.func,
};

export default SwitchLoadingComponent;
